package com.bocai.freeman.base;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.bocai.freeman.R;
import com.jakewharton.rxbinding.view.RxView;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import java.util.concurrent.TimeUnit;

import rx.Observable;


public abstract class BaseActivity extends RxAppCompatActivity implements BaseView {
    private ProgressDialog mDialog;
    private Toast mToast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    protected void initView() {
    }

    protected void initEvent() {
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_remain);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
        super.startActivityForResult(intent, requestCode, options);
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_remain);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(0, R.anim.slide_right_out);
    }

    @Override
    public void showLoading() {
        if (mDialog == null) {
            mDialog = new ProgressDialog(this);
            mDialog.setMessage(getResources().getString(R.string.loading));
        }
        mDialog.show();
    }

    @Override
    public void hideLoading() {
        if (mDialog != null)
            mDialog.dismiss();
    }

//    @Override
//    public void showError(String msg) {
//        Toast.makeText(BaseActivity.this, msg, Toast.LENGTH_SHORT).show();
//    }

    @Override
    public void showToast(String msg) {
        if (mToast == null) {

            mToast = Toast.makeText(this, msg, Toast.LENGTH_LONG);

        } else {
            mToast.setText(msg);
            mToast.setDuration(Toast.LENGTH_LONG);
        }
        mToast.show();
    }

    @Override
    public void setData(Object obj) {

    }

    public <T extends Object> Observable click(View view) {
        return RxView.clicks(view)
                .throttleFirst(1000, TimeUnit.MILLISECONDS);
    }

    public void Log(String msg){
        Log.e(getClass().getSimpleName(),msg);
    }
}
